package de.gwtprime.client;

import java.util.logging.Logger;

import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.web.bindery.event.shared.EventBus;

import de.gwtprime.client.activities.AppActivityMapper;
import de.gwtprime.client.activities.FooterActivity;
import de.gwtprime.client.activities.NavigationActivity;
import de.gwtprime.client.event.UserLoginAttemptEvent;
import de.gwtprime.client.event.handler.UserLoginAttemptEventHandler;
import de.gwtprime.client.login.AuthenticationManager;
import de.gwtprime.client.places.AppPlaceHistoryMapper;
import de.gwtprime.client.places.WelcomePlace;
import de.gwtprime.client.resources.MyResources;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class BitcoinBoerse implements EntryPoint {

	private Place defaultPlace;
	private SimplePanel appWidget = new SimplePanel();

	Logger logger = Logger.getLogger(BitcoinBoerse.class.getName());
	ClientFactory clientFactory = GWT.create(ClientFactoryImpl.class);
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {

		//GWT.setUncaughtExceptionHandler( new CustomUncaughtExceptionHandler() );
		new AuthenticationManager(clientFactory);
		// Inject the contents of the CSS file
		MyResources.INSTANCE.resetCss().ensureInjected();
		MyResources.INSTANCE.carouselCss().ensureInjected();
		MyResources.INSTANCE.gridCss().ensureInjected();
		MyResources.INSTANCE.myCss().ensureInjected();
		MyResources.INSTANCE.gwtCss().ensureInjected();

		RootPanel.get("footer").add(clientFactory.getFooterView());
		
		RootPanel.get("notificationCenter").add(clientFactory.getNotificationCenter());
		
	
		activityAndPlacesTest();
		
		
		// Currently only logged in users get an channel for push notifications
		clientFactory.getEventBus().addHandler(UserLoginAttemptEvent.TYPE, 
				new UserLoginAttemptEventHandler() {
			
			@Override
			public void onUserLoggedIn(UserLoginAttemptEvent event) {
				
				if(event.getUser()!=null)
					new ChannelManagerClient(clientFactory);
				
			}
		});
	}

	private void activityAndPlacesTest() {

		RootPanel.get("greenHead").add(clientFactory.getGreenHeadView());
		RootPanel.get("navigation").add(clientFactory.getNavigationView());
		new FooterActivity(clientFactory);
		new NavigationActivity(clientFactory);
		//defaultPlace = new AnalyzePlace(clientFactory.getStock().getSymbol());
		defaultPlace = new WelcomePlace("dax");
		//defaultPlace = new ImpressumPlace("name");
		EventBus eventBus = clientFactory.getEventBus();
		PlaceController placeController = clientFactory.getPlaceController();

		ActivityMapper mapper = new AppActivityMapper(clientFactory);
		ActivityManager manager = new ActivityManager(mapper, eventBus);

		manager.setDisplay(appWidget);

		AppPlaceHistoryMapper historyMapper = GWT.create(AppPlaceHistoryMapper.class);
		PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper);

		historyHandler.register(placeController, eventBus, defaultPlace);

		RootPanel.get("mainContent").add(appWidget);

		historyHandler.handleCurrentHistory();

	}
}
